package sk.velkavrana.app.ui;

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import sk.velkavrana.app.context.Context;
import sk.velkavrana.app.model.ConnectionInfo;
import sk.velkavrana.app.service.ExportService;
import sk.velkavrana.app.ui.components.JChooserPanel;
import sk.velkavrana.app.ui.components.JExportProgressBarPanel;
import sk.velkavrana.app.util.UIUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;


/**
 * @author mtymes
 * @since May 25, 2009 2:33:46 AM
 */
public class JExportPanel extends JPanel {

    private JChooserPanel tableChooserPanel;


    private JButton initExportButton() {
        return new JButton(new AbstractAction("Export") {
            public void actionPerformed(ActionEvent e) {
                final Set<String> tables = tableChooserPanel.getValues();

                if (tables.size() == 0) {
                    JOptionPane.showMessageDialog(
                            JExportPanel.this,
                            "No tables are selected for exportData",
                            "Empty selection",
                            JOptionPane.WARNING_MESSAGE
                    );
                } else {
                    Context context = Context.getInstance();
                    final ConnectionInfo connectionInfo = context.getConnectionInfoEx();
                    final String exportDirectory = context.getExportDirectory();
                    final ExportService exportService = context.getExportService();

                    final JExportProgressBarPanel exportProgressBarPanel = new JExportProgressBarPanel();
                    JDialog dialog = new JDialog((Frame) null, "Export tables", true);
                    dialog.getContentPane().setLayout(new BorderLayout());
                    dialog.getContentPane().add(exportProgressBarPanel);
                    dialog.pack();
                    dialog.setSize(400, dialog.getSize().height);

                    Runnable exportRunnable = new Runnable() {
                        public void run() {
                            try {
                                Thread.sleep(500);
                            } catch (InterruptedException e1) {
                                e1.printStackTrace();
                            }

                            exportService.exportData(connectionInfo, tables, exportDirectory, exportProgressBarPanel.getExportDataListener());
                        }
                    };
                    Thread exportThread = new Thread(exportRunnable);
                    exportThread.start();

                    UIUtil.center(dialog);
                    UIUtil.show(dialog);
                }
            }
        });
    }


    private JButton initCloseButton() {
        return new JButton(new AbstractAction("Close") {
            public void actionPerformed(ActionEvent e) {
                getTopLevelAncestor().setVisible(false);
            }
        });
    }


    private JPanel initButtonPanel() {
        FormLayout buttonLayout = new FormLayout(
                "pref:grow, pref,4dlu, pref, 2dlu", // columns
                "4dlu, pref, 2dlu" // rows
        );

        JPanel buttonPanel = new JPanel(buttonLayout);

        CellConstraints cc = new CellConstraints();

        buttonPanel.add(initExportButton(), cc.xy(2, 2));
        buttonPanel.add(initCloseButton(), cc.xy(4, 2));

        return buttonPanel;
    }


    private void init() {
        Context context = Context.getInstance();
        ConnectionInfo connectionInfo = context.getConnectionInfoEx();
        List<String> tables = context.getDatabaseService().getAllTables(connectionInfo);

        FormLayout exportLayout = new FormLayout(
                "2dlu, pref:grow, 2dlu", // columns
                "2dlu, default, pref, 4dlu" // rows
        );

        setLayout(exportLayout);

        CellConstraints cc = new CellConstraints();

        tableChooserPanel = new JChooserPanel("Choose tables for exportData: ", new TreeSet<String>(tables), null);
        add(tableChooserPanel, cc.xy(2, 2));
        add(initButtonPanel(), cc.xy(2, 3));
    }


    public JExportPanel(LayoutManager layout, boolean isDoubleBuffered) {
        super(layout, isDoubleBuffered);

        init();
    }


    public JExportPanel(LayoutManager layout) {
        super(layout);

        init();
    }


    public JExportPanel(boolean isDoubleBuffered) {
        super(isDoubleBuffered);

        init();
    }


    public JExportPanel() {
        init();
    }
}
